package com.wephone.Student.network;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.MD5;
import com.wephone.Student.util.Util;

public class HttpClient {
	private static HttpClient hc;
	private static AsyncHttpClient client;
	
	public static HttpClient getInstance(){
		if(hc==null){
			hc = new HttpClient();
			client = new AsyncHttpClient();
			client.setTimeout(Constants.CONNECT_TIME_OUT);
		}
		return hc;
	}

	public void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		System.out.println("----start get-----"+url);
		String nowTime = ""+System.currentTimeMillis()/1000;
		params.put("t", nowTime);
		params.put("k", MD5.md5(MD5.md5(nowTime+Constants.KEY)));
		params.put("clientVersion", "2.0");
		params.put("clientType", "1");
		params.put("ip", Util.getLocalIpAddress());
		System.out.println("---params---"+params.toString());
		System.out.println("---request---"+Constants.API_SERVER_DOMAIN+url+params);
		client.get(Constants.API_SERVER_DOMAIN+url,params,responseHandler);
	}
	
	public void get2(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		System.out.println("----start get-----"+url);
		String nowTime = ""+System.currentTimeMillis()/1000;
		params.put("t", nowTime);
		params.put("k", MD5.md5(MD5.md5(nowTime+Constants.KEY)));
		params.put("clientVersion", "2.0");
		params.put("clientType", "1");
		params.put("ip", Util.getLocalIpAddress());
//		System.out.println("---params---"+params.toString());
		client.get(Constants.API_SERVER_DOMAIN_2+url,params,responseHandler);
	}
	
	public void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		System.out.println("----start post-----"+url);
		String nowTime = ""+System.currentTimeMillis()/1000;
		params.put("t", nowTime);
		params.put("k", MD5.md5(MD5.md5(nowTime+Constants.KEY)));
		params.put("clientVersion", "2.0");
		params.put("clientType", "1");
		params.put("ip", Util.getLocalIpAddress());
		client.post(Constants.API_SERVER_DOMAIN+url,params,responseHandler);
	}
	
	public void post2(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
		System.out.println("----start post-----"+url);
		String nowTime = ""+System.currentTimeMillis()/1000;
		params.put("t", nowTime);
		params.put("k", MD5.md5(MD5.md5(nowTime+Constants.KEY)));
		params.put("clientVersion", "2.0");
		params.put("clientType", "1");
		params.put("ip", Util.getLocalIpAddress());
		client.post(Constants.API_SERVER_DOMAIN_2+url,params,responseHandler);
	}
	
}
